package com.yayun.merchant.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yayun.common.annotation.Log;
import com.yayun.common.core.controller.BaseController;
import com.yayun.common.core.domain.AjaxResult;
import com.yayun.common.enums.BusinessType;
import com.yayun.merchant.domain.PmsProduct;
import com.yayun.merchant.service.IPmsProductService;
import com.yayun.common.utils.poi.ExcelUtil;
import com.yayun.common.core.page.TableDataInfo;

/**
 * 书籍信息Controller
 * 
 * @author kk
 * @date 2024-12-04
 */
@RestController
@RequestMapping("/merchant/book")
public class PmsProductController extends BaseController
{
    @Autowired
    private IPmsProductService pmsProductService;

    /**
     * 查询书籍信息列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:book:list')")
    @GetMapping("/list")
    public TableDataInfo list(PmsProduct pmsProduct)
    {
        startPage();
        List<PmsProduct> list = pmsProductService.selectPmsProductList(pmsProduct);
        return getDataTable(list);
    }

    /**
     * 导出书籍信息列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:book:export')")
    @Log(title = "书籍信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PmsProduct pmsProduct)
    {
        List<PmsProduct> list = pmsProductService.selectPmsProductList(pmsProduct);
        ExcelUtil<PmsProduct> util = new ExcelUtil<PmsProduct>(PmsProduct.class);
        util.exportExcel(response, list, "书籍信息数据");
    }

    /**
     * 获取书籍信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pmsProductService.selectPmsProductById(id));
    }

    /**
     * 新增书籍信息
     */
    @PreAuthorize("@ss.hasPermi('merchant:book:add')")
    @Log(title = "书籍信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PmsProduct pmsProduct)
    {
        return toAjax(pmsProductService.insertPmsProduct(pmsProduct));
    }

    /**
     * 修改书籍信息
     */
    @PreAuthorize("@ss.hasPermi('merchant:book:edit')")
    @Log(title = "书籍信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PmsProduct pmsProduct)
    {
        return toAjax(pmsProductService.updatePmsProduct(pmsProduct));
    }

    /**
     * 删除书籍信息
     */
    @PreAuthorize("@ss.hasPermi('merchant:book:remove')")
    @Log(title = "书籍信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pmsProductService.deletePmsProductByIds(ids));
    }

    /**
     * 修改书籍上下架状态
     */
    @PreAuthorize("@ss.hasPermi('merchant:book:edit')")
    @Log(title = "书籍上下架", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody PmsProduct pmsProduct)
    {
        return toAjax(pmsProductService.changeStatus(pmsProduct.getId(), pmsProduct.getPublishStatus()));
    }

    /**
     * 前台查询书籍信息列表（无需权限）
     */
    @GetMapping("/front/list")
    public TableDataInfo frontList(PmsProduct pmsProduct)
    {
        startPage();
        // 只查询上架的商品
        pmsProduct.setPublishStatus(1);
        List<PmsProduct> list = pmsProductService.selectPmsProductList(pmsProduct);
        return getDataTable(list);
    }

    /**
     * 前台获取书籍详细信息（无需权限）
     */
    @GetMapping(value = "/front/{id}")
    public AjaxResult frontGetInfo(@PathVariable("id") Long id)
    {
        return success(pmsProductService.selectPmsProductById(id));
    }
}
